package com.chc.web.fee_park.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chc.utils.Msg;
import com.chc.web.fee_park.entity.FeePark;
import com.chc.web.fee_park.entity.FeeParkParm;
import com.chc.web.fee_park.service.FeeParkService;
import com.chc.web.fee_power.entity.FeePower;
import com.chc.web.fee_power.entity.FeePowerParm;
import com.chc.web.fee_power.service.FeePowerService;
import com.chc.web.live_house.entity.LiveHouse;
import com.chc.web.live_house.service.LiveHouseService;
import com.chc.web.live_park.entity.LivePark;
import com.chc.web.live_park.mapper.LiveParkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/01/19/15:21
 * @Description: 停车费管理控制器
 */
@RestController
@RequestMapping("/api/feePark")
public class FeeParkController {

    @Autowired
    private FeeParkService feeParkService;

    @Resource
    private LiveParkMapper liveParkMapper;


    /**
     * 我的车费
     * */
    @GetMapping("/getMyParkFee")
    public Msg getMyParkFee(FeeParkParm feeParkParm){
        //构造分页对象
        IPage<FeePark> page = new Page<>();
        page.setCurrent(feeParkParm.getCurrentPage());
        page.setSize(feeParkParm.getPageSize());
        //查询条件
        QueryWrapper<FeePark> query = new QueryWrapper<>();
        query.lambda().eq(FeePark::getUserId,feeParkParm.getUserId());
        IPage<FeePark> list = feeParkService.page(page, query);
        return Msg.success("查询成功",list);
    }

    /**
     * 新增：
     * 1.查询当前正在使用车位的用户
     * 2.入库
     */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:feePark:add')")
    public Msg add(@RequestBody @Valid FeePark feePark){
        //1.查询当前正在使用车位的租户
        //构造查询条件
        QueryWrapper<LivePark> query = new QueryWrapper();
        query.lambda().eq(LivePark::getParkId,feePark.getParkId())
                .eq(LivePark::getLiveStatus,"0");
        LivePark livePark = liveParkMapper.selectOne(query);
        if (livePark == null){
            return Msg.error("该车位暂无人员使用，不能添加!");
        }
//        QueryWrapper<FeePark> query2 = new QueryWrapper();
//        query2.lambda().eq(FeePark::getUserId,livePark.getUserId());
//        FeePark one = feeParkService.getOne(query2);
//        if (one != null){
//            return Msg.error("该车位已添加停车费，不能重复操作!");
//        }else {
//            //把查询出来的用户id设置到电费实体里面
//            feePark.setUserId(livePark.getUserId());
//            //保存数据到数据库
//            boolean save = feeParkService.save(feePark);
//            if(save){
//                return Msg.success("新增停车费成功!");
//            }
//            return Msg.error("新增停车费失败!");
//        }
        List<FeePark> listPower = feeParkService.getListPark(livePark.getUserId());
        if (listPower.size() == 0){
            feePark.setUserId(livePark.getUserId());
            //保存数据到数据库
            feeParkService.save(feePark);
            return Msg.success("新增停车费成功!");
        }
        for (FeePark item : listPower) {
            if (item.getParkId().equals(feePark.getParkId())) {
                if (!item.getPayParkMonth().equals(feePark.getPayParkMonth())) {
                    //把查询出来的用户id设置到电费实体里面
                    feePark.setUserId(livePark.getUserId());
                    //保存数据到数据库
                    feeParkService.save(feePark);
                    return Msg.success("新增停车费成功!");
                }
                return Msg.error("该房屋本月停车费已添加，不能重复操作");
            }
        }
        return Msg.error("新增停车费失败!");
    }

    /**
     * 编辑：
     * 1.查询当前正在使用车位的用户
     * 2.入库
     */
    @PutMapping
    @PreAuthorize("hasAuthority('sys:feePark:edit')")
    public Msg edit(@RequestBody @Valid FeePark feePark){
        //1.查询当前正在使用车位的租户
        //构造查询条件
        QueryWrapper<LivePark> query = new QueryWrapper();
        query.lambda().eq(LivePark::getParkId,feePark.getParkId())
                .eq(LivePark::getLiveStatus,"0");
        LivePark livePark = liveParkMapper.selectOne(query);
        if (livePark == null){
            return Msg.error("该车位暂无人员使用，不能编辑!");
        }
        //把查询出来的用户id设置到停车费实体里面
        feePark.setUserId(feePark.getUserId());
//        保存数据到数据库
        boolean save = feeParkService.updateById(feePark);
        if(save){
            return Msg.success("编辑停车费成功!");
        }
        return Msg.error("编辑停车费失败!");
    }

    /**
     * 删除： 1.判断是否已经缴费，如果缴费，不能删除
     *        2.删除
     */
    @DeleteMapping("/{parkFeeId}")
    @PreAuthorize("hasAuthority('sys:feePark:delete')")
    public Msg delete(@PathVariable("parkFeeId") Long parkFeeId){
        //1.判断是否缴费成功，如果已经缴费，不能删除
        QueryWrapper<FeePark> query = new QueryWrapper();
        query.lambda().eq(FeePark::getParkFeeId,parkFeeId)
                .eq(FeePark::getPayParkStatus,"1");
        FeePark one = feeParkService.getOne(query);
        if (one != null){
            return Msg.error("已缴费，不能删除该数据!");
        }
        //2.删除
        boolean b = feeParkService.removeById(parkFeeId);
        if(b){
            return Msg.success("删除停车费成功!");
        }
        return Msg.error("删除停车费失败!");
    }

    /**
     * 缴费
     * */
    @GetMapping("/pay")
    public Msg payPark( FeePark feePark){
        //把状态设置为1
        feePark.setPayParkStatus("1");
        boolean b = feeParkService.updateById(feePark);
        if(b){
            return Msg.success("缴费成功!");
        }
        return Msg.error("缴费失败!");
    }

    /**
     * 查询列表
     * */
    @GetMapping("/list")
    public Msg getList(@Valid FeeParkParm feeParkParm){
        IPage<FeePark> list = feeParkService.getList(feeParkParm);
        return Msg.success("查询成功",list);
    }
}
